local BaseWnd = require(SCRIPTS_DIR .. ".wnds.BaseWnd");
local Click = require(SCRIPTS_DIR .. ".uikits.Click");

local PageViewCell = class("PageViewCell",function()
    return display.newNode();
end);

function PageViewCell:ctor(i)
    local bg = display.newSprite(mgrs.res:getPngPath("ui_dwc_lobby_base0" .. i));
    local size = bg:getContentSize();
    self:setContentSize(size);
    bg:setPosition(cc.p(size.width/2,size.height/2));
    self:addChild(bg);
    self:setTouchEnabled(false);
    local click = Click.new({targetNode = bg,scale = 0.98}):clickHandler(function()
        mgrs.log:printDebug("PageViewCell")
    end);
    click:setSwallowEnabled(false)
    self.click = click;
end
function PageViewCell:setTouchTarget()
    self.click:setTouchTarget(false);
end


local ScrollCell = class("ScrollCell",function()
    return display.newNode();
end);
function ScrollCell:ctor(index)
    local sprite = cc.Sprite:create(mgrs.res:getPngPath("ui_dwc_lobby_base0" .. index))
    local size = sprite:getContentSize();
    self:setContentSize(size);
    sprite:setPosition(cc.p(size.width/2,size.height/2));
    self:addChild(sprite);
    local click = Click.new({targetNode = sprite});
    click:clickHandler(function()
        mgrs.log:printDebug("index = %d",index)
    end);
    click:setSwallowEnabled(false);
    self.click = click;
end

function ScrollCell:setTouchTarget()
    self.click:setTouchTarget(false);
end


local HallWnd = class("HallWnd",BaseWnd);
function HallWnd:ctor()
    HallWnd.super.ctor(self);
    self.gold = nil
    self.diamond = nil
    self.vipnum = nil
    self.scrollCells = {}
    self.pageCells = {}
end

function HallWnd:onOpen()
    self:setBaseInfo()
    self:setIconAni()
    self:setButtonEvent()
    self:setPageView()
    self:setScrollView()
    self.schedulemail = mgrs.scheduler:scheduleGlobal(handler(self,self.updatemail),50);
end

function HallWnd:updatemail()
    mgrs.data:getLoginData():sendGetMail()
end

function HallWnd:updatepage()

end

function HallWnd:setEvent()
    -- body
end

function HallWnd:setBaseInfo()
    -- body
    self.vip = self:widget("left.vipnum")
    self.gold = self:widget("left.gold")
    self.diamond = self:widget("left.diamond")
    local playerdata = mgrs.data:getPlayerData()
    self:widget("left.name"):setString(playerdata:getNickName())

    local v = playerdata:getVip()
    if v == 0 then
        self.vip:setVisible(false)
        self:widget("left.vip"):setVisible(false)
        self:widget("left.vip0"):setVisible(true)
    else
        self.vip:setVisible(true)
        self.vip:setString(v)
        self:widget("left.vip"):setVisible(true)
        self:widget("left.vip0"):setVisible(false)
    end
    self.gold:setString(playerdata:getGold())
    self.diamond:setString(playerdata:getDiamond())
    
    self:addEventListener(enums.Event.GoldChange,function()
        self.gold:setString(playerdata:getGold())
    end)
    self:addEventListener(enums.Event.DiamondChange,function()
        self.diamond:setString(playerdata:getDiamond())
    end)
    self:addEventListener(enums.Event.VipChange,function()
        self.vip:setString(playerdata:getVip())
    end)
end

function HallWnd:setPageView()
    local pv = self:widget("left.pageview")
    local pageview = self:createPageView(
    {
        pv = pv,
        col = 1,
        row = 1,
        columnSpace = 1,
        rowSpace = 1,
        bCirc = true,
        autoMark = false
    });

    for i=1,3 do
        local cell = PageViewCell.new(i)
        table.insert(self.pageCells,cell);
        pageview:addContent(cell);
    end
    pageview:setPageChangedCbk(function(event) 
        print("11111111111111")
    end);
    pageview:setItemClickedCbk(function(event)
        print("2222222222222222222222222")
    end)
    pageview:setCellMovedCbk(function(event)
        for key, var in pairs(self.pageCells) do
            var:setTouchTarget();
        end
    end)
    pageview:reload();
    pageview:gotoPage(1,true,true);
    self.schedulepage = mgrs.scheduler:scheduleGlobal(function()
        pageview:gotoNextPage()
    end,5);
end

function HallWnd:setScrollView()
    local sv = self:widget("middle.scrollview");
    local scrollView = self:createScrollView({sv = sv,dis = 30});
    for i=1,6 do
        local cell = ScrollCell.new(i);
        scrollView:addContent(cell);
        table.insert(self.scrollCells,cell);
    end
    scrollView:setOnScrollCbk(function(event)
        if event.name == "moved" then
            for key, var in pairs(self.scrollCells) do
                var:setTouchTarget();
            end
        end
    end);
    scrollView:addScrollNode();
end

function HallWnd:setIconAni()
    local goldicon = self:widget("left.goldicon")
    mgrs.animate:createAnimate({name = "gold",frameName = "ui_dwc_lobby_gold_eft_%d.png",start = 1,endIndex = 9,time = 0.8});
    mgrs.action:beginAction(goldicon):PlayAnimate("gold",false):DelayTime(1):Sequence(2):RepeatForever():endAction();

    local diamondicon = self:widget("left.diamondicon")
    mgrs.animate:createAnimate({name = "diamond",frameName = "dwc_lobby_dimond_eft_%d.png",start = 1,endIndex = 8,time = 0.8});
    mgrs.action:beginAction(diamondicon):PlayAnimate("diamond",false):DelayTime(1):Sequence(2):RepeatForever():endAction();

    local trolley = self:widget("right.shop.trolley")
    mgrs.animate:createAnimate({name = "trolley",frameName = "ui_dwc_lobby_eft_2_%d.png",start = 1,endIndex = 18,time = 1.2});
    mgrs.action:beginAction(trolley):PlayAnimate("trolley",false):DelayTime(0.5):Sequence(2):RepeatForever():endAction();

    local enter = self:widget("right.shop.enter")
    mgrs.animate:createAnimate({name = "enter",frameName = "ui_dwc_lobby_eft_1_%d.png",start = 1,endIndex = 3,time = 0.8});
    mgrs.action:beginAction(enter):PlayAnimate("enter",false):DelayTime(0.5):Sequence(2):RepeatForever():endAction();

    local share = self:widget("left.share.shareicon")
    mgrs.action:beginAction(share):RotateBy(0.05,-2):RotateBy(0.1,4):RotateBy(0.08,-3):RotateBy(0.03,2):RotateBy(0.01,-1):DelayTime(3):Sequence(6):RepeatForever():endAction();

    local shareaction = self:widget("left.share.action")
    mgrs.animate:createAnimate({name = "sharegold",frameName = "ui_dwc_lobby_share_gold_eft_%d.png",start = 1,endIndex = 21,time = 0.8});
    mgrs.action:beginAction(shareaction):PlayAnimate("sharegold",false):DelayTime(2):Sequence(2):RepeatForever():endAction();
end


function HallWnd:setButtonEvent()
    -- body
    local photo = self:widget("left.photobg")
    self:createClick({targetNode = photo}):clickHandler(function()
        mgrs.log:printDebug("photo")
        mgrs.data:getLoginData():useProp(1009001,2)
    end);

    local btngold = self:widget("left.btngold")
    self:createClick({targetNode = btngold}):clickHandler(function()
        mgrs.log:printDebug("btngold")
        mgrs.wnd:openWnd(enums.Wnd.Pay,{tradeid = 1002})
    end);

    local btndiamond = self:widget("left.btndiamond")
    self:createClick({targetNode = btndiamond}):clickHandler(function()
        mgrs.log:printDebug("btndiamond")
        mgrs.wnd:openWnd(enums.Wnd.Pay,{tradeid = 1003})
    end);

    local sign = self:widget("right.signnode")
    self:createClick({targetNode = sign}):clickHandler(function()
        mgrs.log:printDebug("sign")
        mgrs.data:getLoginData():sendSignInfo()
    end);

    local fcharge = self:widget("right.fchargenode")
    self:createClick({targetNode = fcharge}):clickHandler(function()
        mgrs.log:printDebug("fcharge")
    end);

    local shop = self:widget("right.shop")
    self:createClick({targetNode = shop}):clickHandler(function()
        mgrs.log:printDebug("shop")
    end);

    local share = self:widget("left.share")
    self:createClick({targetNode = share}):clickHandler(function()
        mgrs.log:printDebug("share")
        mgrs.data:getLoginData():sendBindInviteCode()
    end);
end

function HallWnd:onClose()
    mgrs.scheduler:unscheduleGlobal(self.schedulemail);
    mgrs.scheduler:unscheduleGlobal(self.schedulepage);
    mgrs.animate:removeAnimation("gold")
    mgrs.animate:removeAnimation("diamond")
    mgrs.animate:removeAnimation("trolley")
    mgrs.animate:removeAnimation("enter")
    mgrs.animate:removeAnimation("sharegold")
    
end


function HallWnd:reqData(handler)
	handler(true);	
end

function HallWnd:getLayer()
	return enums.Layer.Window;
end

function HallWnd:getPreloadResList()
	return {Plist = {"gold","diamond","shoptrolley","shopenter","sharegold"}};
end

function HallWnd:getExclusiveType()
	return 1;
end

function HallWnd:isOpenCloseSoundEnabled()
	return false;
end

function HallWnd:onEnter()
	mgrs.log:printDebug("HallWnd:onEnter");
end
function HallWnd:onExit()
	mgrs.log:printDebug("HallWnd:onExit");
end
function HallWnd:onEnterTransitionFinish()
	mgrs.log:printDebug("HallWnd:onEnterTransitionFinish");
end

function HallWnd:onExitTransitionStart()
	mgrs.log:printDebug("HallWnd:onExitTransitionStart");
end


mgrs.wnd:registerWnd(enums.Wnd.Hall,HallWnd);

--endregion
